<?php

App::uses('CrudController', 'Controller');
App::uses('Test', 'Model');

class AttemptsController extends CrudController
{
    public $_model = 'Attempt';

    public function admin($userId) {
        $this->request->allowMethod('get');
        $this->loadModel('User');
        $this->User->id = $userId;
        $user = $this->User->read();
        $this->loadModel('Test');
        $tests = $this->Test->getEnabled();
        $appBosch = new stdClass();
        $appBosch->userId = $userId;
        $appBosch->tests = $tests;
        $appBosch->users = array(array('text' => $user['User']['full_name'], 'value' => $user['User']['id']));
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Cursos'));
        $this->set('description', __('Administración'));
    }

    public function getAttempts($userId, $testId)
    {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $records = $this->$m->getByUserIdAndTestId($userId, $testId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getAttemptsDetails($evaluation)
    {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $records = $this->$m->getEnabledByEvaluation($evaluation);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function undated() {
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Reporte de cursos'));
        $this->set('description', __('Sin fecha'));
    }

    public function getUndated() {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $userId = $this->Auth->user('id');
        $records = $this->$m->getEnabledUndated($userId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    /**
     * Actualizamos la fecha de un intento desde el reporte de intentos sin fechas
     */
    public function updateDate() {
        $this->request->allowMethod('get');
        $att = $this->request->query['models'];
        $att = json_decode($att);
        $m = $this->_model;
        $date = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $att->date_att);
        $attUp = array(
            'id' => $att->value,
            'application_date' => $date->format('Y-m-d'),
            'date_att' => $date->format('d/m/Y'),
        );
        $this->$m->save($attUp);
        $attUp['nomina'] = $att->nomina;
        $attUp['name'] = $att->name;
        $attUp['calif'] = $att->calif;
        $attUp['nameTest'] = $att->nameTest;
        $this->set(array('records' => $attUp, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($userId)
    {
        $m = $this->_model;
        $records = $this->$m->getTestsByUserId($userId);
        return $records;
    }

    protected function c($model)
    {
        return array(
            'user_id' => trim($model->user_id),
            'test_id' => trim($model->test_id),
            'application_date' => $model->application_date,
            'calification' => (float)$model->calification,
        );
    }

    protected function u($model)
    {
        return array(
            'user_id' => trim($model->user_id),
            'test_id' => trim($model->test_id),
            'application_date' => $model->application_date,
            'calification' => (float)$model->calification,
        );
    }
}
